package com.gdyx.app.web.web;


import com.alibaba.fastjson.JSONObject;
import com.gdyx.app.web.annotation.Authorisation;
import com.gdyx.app.web.dto.ZTreeNode;
import com.gdyx.app.web.model.MpsFun;
import com.gdyx.app.web.model.MpsFunOpt;
import com.gdyx.app.web.model.MpsUser;
import com.gdyx.app.web.svc.MpsFunOptSvc;
import com.gdyx.app.web.svc.MpsFunSvc;
import com.gdyx.app.web.util.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/MpsOpts")
@Scope("prototype")
public class MpsOptController extends BaseController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    MpsFunOptSvc mpsFunOptSvc;
    @Autowired
    MpsFunSvc mpsFunSvc;

    @Authorisation(value = "/MpsOpts/goMpsOptsList")
    @GetMapping("/goMpsOptsList")
    public ModelAndView goMpsOptsList(@ModelAttribute("respMsg") String respMsg) {
        ModelAndView view = null;
        request.getSession().setAttribute("AllFuns", MpsOptsListFindAllFun());
        try {
            //默认加载项
            MpsFun mpsFun = mpsFunSvc.findMpsFunByHref("/MpsUser/goMpsUserList");
            view = new ModelAndView();
            Map<String, Object> map = new HashMap<>();
            List<Condition> conditions = new ArrayList<>();
            conditions.add(ruleBuild("and", "funId", "=", mpsFun.getId(), "S"));
            map.put("conditions", conditions);
            //获得指定funId下的所有操作
            List<MpsFunOpt> mpsFunOptsList = mpsFunOptSvc.findMpsFunOpts(map);
            view.addObject("mpsFunOptsList", mpsFunOptsList);
            //模板嵌套区分
            view.addObject("pageSig", "MpsOptsFunList");
            //头信息
            view.addObject("headerMess", mpsFun.getName());
            view.addObject("f_I", mpsFun.getId());
            //返回页面
            view.setViewName("MpsOptsList");
            view.addObject("respMsg", respMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Authorisation(value = "/MpsOpts/Update")
    @GetMapping("/goMpsOptsEdit")
    public ModelAndView goMpsOptsEdit(@ModelAttribute("respMsg") String respMsg, @RequestParam("id") String id) {
        ModelAndView view = new ModelAndView();
        MpsFunOpt mpsFunOpt = mpsFunOptSvc.findMpsFunOptById(id);
        if (null == mpsFunOpt) {
            view.addObject("respMsg", " 修改操作不存在");
            view.setViewName("redirect:/MpsOpts/goMpsOptsList");
            return view;
        } else {
            MpsFun mpsFun = mpsFunSvc.findMpsFunById(mpsFunOpt.getFunId());
            view.addObject("headerMess", mpsFun.getName());
            view.addObject("O_I", mpsFunOpt);
            view.addObject("pageSig", "MpsOptsEdit");
            view.addObject("respMsg", respMsg);
            view.setViewName("MpsOptsList");
            return view;
        }
    }

    @Authorisation(value = "/MpsOpts/Save")
    @GetMapping("/goMpsOptsAdd")
    public ModelAndView goMpsOptsAdd(@ModelAttribute("respMsg") String respMsg, @RequestParam("id") String id) {
        ModelAndView view = new ModelAndView();
        MpsFun mpsFun = mpsFunSvc.findMpsFunById(id);
        view.addObject("headerMess", mpsFun.getName());
        view.addObject("f_I", id);
        view.addObject("respMsg", respMsg);
        view.addObject("pageSig", "MpsOptsAdd");
        view.setViewName("MpsOptsList");
        return view;
    }

    @Authorisation(value = "/MpsOpts/Save")
    @PostMapping("/Save")
    public String save(MpsFunOpt opts, RedirectAttributes attr) {
        if (StringUtils.isEmpty(opts.getName())) {
            attr.addFlashAttribute("respMsg", "操作添加失败，操作名称不能为空");
            return "redirect:/MpsOpts/goMpsOptsAdd?id=" + opts.getFunId();
        }
        if (StringUtils.isEmpty(opts.getHref())) {
            attr.addFlashAttribute("respMsg", "操作添加失败，操作连接不能为空");
            return "redirect:/MpsOpts/goMpsOptsAdd?id=" + opts.getFunId();
        }
        Map<String, Object> map = new HashMap<>();
        List<Condition> conditions = new ArrayList<>();
        conditions.add(ruleBuild("and", "name", "=", opts.getName(), "S"));
        conditions.add(ruleBuild("and", "funId", "=", opts.getFunId(), "S"));
        map.put("conditions", conditions);
        int count = mpsFunOptSvc.count(map);
        if (count > 0) {
            attr.addFlashAttribute("respMsg", "操作添加失败，操作名称已存在");
            return "redirect:/MpsOpts/goMpsOptsAdd?id=" + opts.getFunId();
        }
        ModelAndView view = new ModelAndView();
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        opts.setCreateOwner(mpsUser.getId());
        mpsFunOptSvc.save(opts);
        attr.addFlashAttribute("respMsg", "添加操作: " + "\"" + opts.getName() + "\"" + " 成功");
        return "redirect:/MpsOpts/MpsOptsListFindByFunId?funId=" + opts.getFunId();

    }

    @Authorisation(value = "/MpsOpts/Delete")
    @GetMapping("/Delete")
    public ModelAndView delete(@RequestParam("id") String id, RedirectAttributes attr) {
        ModelAndView view = new ModelAndView();
        try {
            //根据id查询
            MpsFunOpt mpsFunOpt = mpsFunOptSvc.findMpsFunOptById(id);
            mpsFunOptSvc.deleteOptsAndRoleOpts(id);
            attr.addFlashAttribute("respMsg", "删除操作成功");
            view.setViewName("redirect:/MpsOpts/MpsOptsListFindByFunId?funId=" + mpsFunOpt.getFunId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Authorisation(value = "/MpsOpts/Update")
    @PostMapping("/Update")
    public String update(MpsFunOpt opts, RedirectAttributes attr) {
        if (StringUtils.isEmpty(opts.getName())) {
            attr.addFlashAttribute("respMsg", "操作修改失败，操作名称不能为空");
            return "redirect:/MpsOpts/goMpsOptsEdit?id=" + opts.getId();
        }
        if (StringUtils.isEmpty(opts.getHref())) {
            attr.addFlashAttribute("respMsg", "操作修改失败，操作连接不能为空");
            return "redirect:/MpsOpts/goMpsOptsEdit?id=" + opts.getId();
        }
        Map<String, Object> map = new HashMap<>();
        List<Condition> conditions = new ArrayList<>();
        conditions.add(ruleBuild("and", "id", "<>", opts.getId(), "<>"));
        conditions.add(ruleBuild("and", "name", "=", opts.getName(), "="));
        map.put("conditions", conditions);
        int count = mpsFunOptSvc.count(map);
        if (count > 0) {
            attr.addFlashAttribute("respMsg", "操作修改失败，操作名称已存在");
            return "redirect:/MpsOpts/goMpsOptsEdit?id=" + opts.getId();
        }
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        opts.setModifyOwner(mpsUser.getId());
        mpsFunOptSvc.update(opts);
        attr.addFlashAttribute("respMsg", "修改操作: " + "\"" + opts.getName() + "\"" + " 成功");
        return "redirect:/MpsOpts/MpsOptsListFindByFunId?funId=" + opts.getFunId();

    }


    @Authorisation(value = "/MpsOpts/goMpsOptsList")
    @GetMapping("/MpsOptsListFindByFunId")
    public ModelAndView MpsOptsListFindByFunId(@RequestParam("funId") String funId, @ModelAttribute("respMsg") String respMsg) {
        ModelAndView view = new ModelAndView();
        try {
            Map<String, Object> map = new HashMap<>();
            List<Condition> conditions = new ArrayList<>();
            conditions.add(ruleBuild("and", "funId", "=", funId, "S"));
            map.put("conditions", conditions);
            //获得指定funId下的所有操作
            List<MpsFunOpt> mpsFunOptsList = mpsFunOptSvc.findMpsFunOpts(map);
            //获取指定funId下的操作页面头信息
            MpsFun mpsFun = mpsFunSvc.findMpsFunById(funId);
            view.addObject("headerMess", mpsFun.getName());
            view.addObject("mpsFunOptsList", mpsFunOptsList);
            view.addObject("f_I", funId);
            view.addObject("pageSig", "MpsOptsFunList");
            view.addObject("respMsg", respMsg);
            view.setViewName("MpsOptsList");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    public String MpsOptsListFindAllFun() {
        List<ZTreeNode> zTreeResultList = new ArrayList<>();
        //所有节点信息
        List<MpsFun> mpsFuns = mpsFunSvc.findMpsFuns(new HashMap<String, Object>());
        for (MpsFun mpsFun : mpsFuns) {
            ZTreeNode zTreeNode = new ZTreeNode();
            zTreeNode.setId(mpsFun.getId());
            zTreeNode.setpId(mpsFun.getParent());
            zTreeNode.setName(mpsFun.getName());
            zTreeResultList.add(zTreeNode);
            zTreeNode.setOpen(true);
        }
        System.out.println(JSONObject.toJSONString(zTreeResultList));
        return JSONObject.toJSONString(zTreeResultList);
    }
}